<?php

namespace app\api\controller\Card;

use think\Controller;
use think\Request;

class Card extends Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->TypeModel = model('Card.Type');
        $this->OrderModel = model('Card.Order');
        $this->CardModel = model('Card.Card');
    }

    // 分类接口
    public function type()
    {
        $typelist = $this->TypeModel->select();
        $this->success('查询成功', null, $typelist);
    }

    // 当前登录用户的生成名片
    public function card()
    {
        $userid = $this->request->param('userid');
        $card = $this->CardModel->with('type')->where(['userid' => $userid])->find();
        $this->success('查询成功', null, $card);
    }

    // 生成名片
    public function add()
    {
        $params = $this->request->param();
        $card = $this->CardModel->where(['id' => $params['cardid'], 'userid' => $params['userid']])->find();
        if ($card) {
            // 更新
            $data = [
                'id' => $params['cardid'],
                'userid' => $params['userid'],
                'nickname' => $params['nickname'],
                'mobile' => $params['mobile'],
                'email' => $params['email'],
                'sex' => $params['sex'],
                'content' => $params['content'],
                'typeid' => $params['typeid'],
                'price' => 20
            ];

            if (!empty($params['code'])) {
                $parentpath = model('Region')->where(['code' => $params['code']])->value('parentpath');

                if (!$parentpath) {
                    $this->error('所选地区不存在');
                }

                [$province, $city, $district] = explode(',', $parentpath);

                if ($province) {
                    $data['province'] = $province;
                }

                if ($city) {
                    $data['city'] = $city;
                }

                if ($district) {
                    $data['district'] = $district;
                }
            }

            if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {
                $avatar = upload('avatar');

                if ($avatar['code'] === 1) {
                    $data['avatar'] = $avatar['data'];
                }
            }

            $result = $this->CardModel->isUpdate(true)->save($data);

            if ($result === FALSE) {
                if (!empty($data['avatar']) && $_FILES['avatar']['size'] > 0) {
                    @is_file('.' . $data['avatar']) && unlink('.' . $data['avatar']);
                }
                $this->error($this->CardModel->getError());
            } else {
                $user = model('User.User')->find($params['userid']);

                if (!empty($data['avatar']) && $_FILES['avatar']['size'] > 0 && $card['avatar'] != $user['avatar']) {
                    @is_file('.' . $card['avatar']) && unlink('.' . $card['avatar']);
                }
                $this->success('生成成功');
            }
        } else {
            // halt("新增");
            $data = [
                'userid' => $params['userid'],
                'nickname' => $params['nickname'],
                'mobile' => $params['mobile'],
                'email' => $params['email'],
                'sex' => $params['sex'],
                'content' => $params['content'],
                'typeid' => $params['typeid'],
                'price' => 20
            ];

            if (!empty($params['code'])) {
                $parentpath = model('Region')->where(['code' => $params['code']])->value('parentpath');

                if (!$parentpath) {
                    $this->error('所选地区不存在');
                }

                [$province, $city, $district] = explode(',', $parentpath);

                if ($province) {
                    $data['province'] = $province;
                }

                if ($city) {
                    $data['city'] = $city;
                }

                if ($district) {
                    $data['district'] = $district;
                }
            }

            if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {
                $avatar = upload('avatar');

                if ($avatar['code'] === 1) {
                    $data['avatar'] = $avatar['data'];
                }
            } else {
                $user = model('User.User')->find($params['userid']);

                if ($user) {
                    $data['avatar'] = $user['avatar'];
                }
            }

            $result = $this->CardModel->validate('common/Card/Card')->save($data);

            if ($result === FALSE) {
                if (!empty($data['avatar']) && $_FILES['avatar']['size'] > 0) {
                    @is_file('.' . $data['avatar']) && unlink('.' . $data['avatar']);
                }
                $this->error($this->CardModel->getError());
            } else {
                $this->success('生成成功');
            }
        }
    }
}
